const menu = {

	async getRepository() {
		let list = []
		await uni.$http.get("/menu/getFridgeRepository?fid=" + uni.$store.state.fridge.default_fridge.fid)
			.then(({
				data: res
			}) => {
				if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
				res.data.forEach(x => {
					x.imgSrc = uni.$staticResourcePath + x.imgSrc
				})
				list = res.data
			})
		return list
	},

	async getAllMenu() {
		let list = []
		await uni.$http.get("/menu/GetAllMenu")
			.then(({
				data: res
			}) => {
				if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
				res.data.forEach(x => {
					x.imgSrc = uni.$staticResourcePath + x.imgSrc
					x.tags = x.tagList || ['家常', '高分']
					x.rate = x.score || 4.5
				})
				list = res.data
			})
		return list
	},

	/**
	 * 获取冰箱库存的指定物品可组成的菜谱
	 * @param {Object} otId
	 */
	async getDishesByObjType(otId) {
		let list = []
		await uni.$http.get("/menu/getDishesByObjType?otId=" + otId)
			.then(({
				data: res
			}) => {
				if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
				res.data.forEach(x => {
					x.imgSrc = uni.$staticResourcePath + x.imgSrc
					x.tags = x.tagList || ['家常', '高分']
					x.rate = x.score || 4.5
				})
				list = res.data
			})
		return list
	},

	/**
	 * 获取冰箱内已有物资可组成的菜谱 V5
	 */
	async getFridgeMenu() {
		let list = []
		await uni.$http.get("/menu/getFridgeMenu?fid=" + uni.$store.state.fridge.default_fridge.fid)
			.then(({
				data: res
			}) => {
				if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
				if (res.data.length != 0)
					res.data.forEach(x => {
						x.imgSrc = uni.$staticResourcePath + x.imgSrc
						x.materials.forEach((y) => {
							y.imgSrc = uni.$staticResourcePath + y.imgSrc
						})
						x.tags = x.tagList || ['家常', '高分']
						x.rate = x.score || 4.5
					})
				list = res.data
			})
		return list
	},

	/**
	 * 获取推荐菜谱 V6
	 */
	async getRecommendedMenu() {
		let list = []
		await uni.$http.get("/menu/getRecommendedMenu")
			.then(({
				data: res
			}) => {
				if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
				if (res.data.length != 0)
					res.data.forEach(x => {
						x.imgSrc = uni.$staticResourcePath + x.imgSrc
						x.tags = x.tagList || ['家常', '高分']
						x.rate = x.score || 4.5
					})
				list = res.data
			})
		return list
	},

	/**
	 * 获取指定类型菜谱 V6
	 * @param {Object} type
	 */
	async getMenuOfType(type) {
		let list = []
		await uni.$http.get("/menu/getMenuOfType?type=" + type)
			.then(({
				data: res
			}) => {
				if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
				if (res.data.length != 0)
					res.data.forEach(x => {
						x.imgSrc = uni.$staticResourcePath + x.imgSrc
						x.tags = x.tagList || ['家常', '高分']
						x.rate = x.score || 4.5
					})
				list = res.data
			})
		return list
	},

	/**
	 * 菜谱详情 V6
	 * @param {Object} menuId
	 */
	async getMenuDetail(menuId) {
		let menu = {}
		await uni.$http.get("/menu/getMenuDetail?menuId=" + menuId)
			.then(({
				data: res
			}) => {
				if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
				res.data.imgSrc = uni.$staticResourcePath + res.data.imgSrc
				res.data.madeNum = res.data.doNum || 123

				res.data.detail.recipeList.forEach(x => {
					if (x.num == 0) { // x.num = parseInt(Math.random() * 10 + 0)
						x.need = "若干"
						x.num = 1
					} else
						x.need = x.num + x.unit
					if (!x.imgSrc) {
						x.imgSrc = "/resources/images/objectImg/鸡蛋.png"
					}
					x.imgSrc = uni.$staticResourcePath + x.imgSrc
				})
				res.data.materials = res.data.detail.recipeList

				res.data.rate = res.data.averageScore || 4.5

				res.data.difficulty = res.data.difficulty || '易上手'
				res.data.duration = res.data.duration || '10-30分钟'

				res.data.detail.stepList?.forEach(y => {
					y.imgSrc = y.img
				})
				res.data.steps = res.data.detail.stepList
				menu = res.data
			})
		return menu
	}
}

export default menu